<!DOCTYPE html>
<html lang="en">
<head>
    {% load static %}
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="referrer" content="no-referrer"/>
    <title>BT解析分享</title>
    <link rel="icon" href="{% static 'favicon.ico' %}" type="image/x-icon">
    <link rel="stylesheet" href="{% static 'layui/css/layui.css' %}" media="all"/>
    <script src="{% static 'layui/layui.js' %}" charset="utf-8"></script>
    <script>
        tab = '{{ tab }}';
        main = '{{ main }}';
        page = {{ page }};

        function jumpToUrl(page) {
            let kwd = document.getElementById('kwd').value.replace(/^\s+|\s+$/g, '');
            let url = '';
            url += 'tab=' + tab;
            url += kwd ? '&kwd=' + kwd : '';
            url += page ? '&page=' + page : '';
            url = url ? '?' + url : '';
            window.location.href = '/' + url
        }

        function request(method, url, success, fail) {
            if (!method) method = 'GET';
            if (!url) return false;
            let http = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
            http.onreadystatechange = function () {
                if (http.readyState === 4 && http.status === 200) {
                    if (success) success(http.responseText)
                } else {
                    if (fail) fail()
                }
            }
            http.open(method, url, true);
            http.send();
            return true
        }

        function key_down(e) {
            if (e.keyCode === 13 || !e) {
                jumpToUrl(page)
            }
        }

        function show_dialog(title, width, height, content, autoClose) {
            let layer = layui.layer;
            //多窗口模式，层叠置顶
            layer.closeAll();
            layer.open({
                type: 2, //此处以iframe举例
                title: title,
                skin: 'layui-layer-molv', //样式类名
                area: [width, height],
                offset: 'rb',
                anim: 2,
                shade: 0,
                maxmin: true,
                content: content,
                time: autoClose ? 1000 : 0,
                yes: function () {
                    layer.closeAll()
                },
                zIndex: layer.zIndex,
                success: function (layero) {
                    layer.setTop(layero);
                    if (autoClose) {
                        layer.closeAll()
                    }
                }
            })
        }

        function dismiss(url) {
            if (url) {
                show_dialog('准备下载', '300px', '100px', url, true)
            } else {
                layui.layer.closeAll()
            }
        }

        function show_page(url) {
            show_dialog('原始网页', '1080px', '90%', main + url)
        }

        function show_download(url) {
            show_dialog('下载地址', '800px', '400px', '/download/?tab=' + tab + '&url=' + url)
        }

        function tabChange(tab) {
            this.tab = tab;
            jumpToUrl(page)
        }

        layui.use(['laypage', 'layer'], function () {
            layui.laypage.render({
                elem: 'pages',
                count: {{ ttp }}0,
                curr: page,
                jump: function (obj) {
                    if (obj.curr !== page) {
                        jumpToUrl(obj.curr)
                    }
                }
            });
        });

    </script>
</head>
<body>
<ul class="layui-nav layui-bg-green">
    {% for item in tabs %}
        <li class="layui-nav-item{{ item.this }}"><a onclick="tabChange('{{ item.tab }}')"
                                                     style="cursor:pointer">{{ item.title }}</a></li>
    {% endfor %}

    <div style="float: right; margin-top: 10px">
        <div class="layui-inline">
            <label for="kwd" class="layui-form-label">搜索关键字</label>
        </div>
        <div class="layui-inline">
            <input id="kwd" placeholder="请输入关键字" class="layui-input" value="{{ kwd }}" onkeydown="key_down(event)"/>
        </div>
        <div class="layui-inline">
            <button onclick="key_down()" class="layui-btn">搜索</button>
        </div>
    </div>
</ul>

<table class="layui-table">
    <thead>
    <tr>
        <th>名称</th>
        <th>网页</th>
    </tr>
    </thead>
    <tbody>
    {% for item in list %}
        <tr>
            <td><a onclick="show_download('{{ item.href }}')" style="cursor:pointer">{{ item.title }}</a></td>
            <td>
                <button onclick="show_page('{{ item.href }}')" class="layui-btn">查看</button>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>
<div id="pages"></div>
</body>
</html>